package com.hongxun.project.assets.pMaterial.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongxun.project.assets.pMaterial.mapper.PMaterialMapper;
import com.hongxun.project.assets.pMaterial.domain.PMaterial;
import com.hongxun.project.assets.pMaterial.service.IPMaterialService;
import com.hongxun.common.utils.text.Convert;

/**
 * 配电材料采集 服务层实现
 *
 * @author hongxun
 * @date 2019-03-19
 */
@Service
public class PMaterialServiceImpl implements IPMaterialService {
    @Autowired
    private PMaterialMapper pMaterialMapper;

    /**
     * 查询配电材料采集信息
     *
     * @param id 配电材料采集ID
     * @return 配电材料采集信息
     */
    @Override
    public PMaterial selectPMaterialById(String id, String taskId) {
        return pMaterialMapper.selectPMaterialById(id, taskId);
    }

    @Override
    public PMaterial selectPMaterialByIdForState(String id, String taskId) {
        return pMaterialMapper.selectPMaterialByIdForState(id, taskId);
    }

    /**
     * 查询配电材料采集列表
     *
     * @param pMaterial 配电材料采集信息
     * @return 配电材料采集集合
     */
    @Override
    public List<PMaterial> selectPMaterialList(PMaterial pMaterial) {
        return pMaterialMapper.selectPMaterialList(pMaterial);
    }

    /**
     * 新增配电材料采集
     *
     * @param pMaterial 配电材料采集信息
     * @return 结果
     */
    @Override
    public int insertPMaterial(PMaterial pMaterial) {
        pMaterial.setId(UUID.randomUUID().toString());
        return pMaterialMapper.insertPMaterial(pMaterial);
    }

    /**
     * 修改配电材料采集
     *
     * @param pMaterial 配电材料采集信息
     * @return 结果
     */
    @Override
    public int updatePMaterial(PMaterial pMaterial) {
        return pMaterialMapper.updatePMaterial(pMaterial);
    }

    /**
     * 删除配电材料采集对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePMaterialByIds(String ids) {
        return pMaterialMapper.deletePMaterialByIds(Convert.toStrArray(ids));
    }


    @Override
    public int deletePMaterialByIdsAndTaskIds(String ids, String taskIds) {
        return pMaterialMapper.deletePMaterialByIdsAndTaskIds(Convert.toStrArray(ids), Convert.toStrArray(taskIds));
    }


    @Override
    public void insertOrReplaceOrDelete(List<PMaterial> entities) {
        for (PMaterial d : entities) {
//			if (d.getDatastate().equals("删除")) {
//				pMaterialMapper.deletePMaterialByIdsAndTaskIds(Convert.toStrArray(d.getId()) , Convert.toStrArray(d.getTaskId()));
//			} else {
            PMaterial pMaterial = pMaterialMapper.selectPMaterialById(d.getId(), d.getTaskId());
            if (pMaterial == null) {
                pMaterialMapper.insertPMaterial(d);
            } else {
                pMaterialMapper.updatePMaterial(d);
//				}
            }
        }
    }

}
